/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hhx.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hhx.entity.HhxInvite;
import com.thinkgem.jeesite.modules.hhx.service.HhxInviteService;
import com.thinkgem.jeesite.modules.hhx.service.HhxUserService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-09
 */
@Controller
@RequestMapping(value = "${adminPath}/hhx/hhxInvite")
public class HhxInviteController extends BaseController {

	@Autowired
	private HhxInviteService hhxInviteService;
	@Autowired
	private HhxUserService hhxUserService;
	
	@ModelAttribute
	public HhxInvite get(@RequestParam(required=false) String id) {
		HhxInvite entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxInviteService.get(id);
		}
		if (entity == null){
			entity = new HhxInvite();
		}
		return entity;
	}
	
	@RequiresPermissions("hhx:hhxInvite:view")
	@RequestMapping(value = {"list", ""})
	public String list(HhxInvite hhxInvite, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxInvite> page = hhxInviteService.findPage(new Page<HhxInvite>(request, response), hhxInvite); 
		model.addAttribute("page", page);
		return "modules/hhx/hhxInviteList";
	}

	@RequiresPermissions("hhx:hhxInvite:view")
	@RequestMapping(value = "form")
	public String form(HhxInvite hhxInvite, Model model) {
		model.addAttribute("hhxInvite", hhxInvite);
		return "modules/hhx/hhxInviteForm";
	}

	@RequiresPermissions("hhx:hhxInvite:edit")
	@RequestMapping(value = "save")
	public String save(HhxInvite hhxInvite, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hhxInvite)){
			return form(hhxInvite, model);
		}
		hhxInviteService.save(hhxInvite);
		addMessage(redirectAttributes, "保存邀约成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxInvite/?repage";
	}
	
	@RequiresPermissions("hhx:hhxInvite:edit")
	@RequestMapping(value = "delete")
	public String delete(HhxInvite hhxInvite, RedirectAttributes redirectAttributes) {
		hhxInviteService.delete(hhxInvite);
		addMessage(redirectAttributes, "删除邀约成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxInvite/?repage";
	}
	
	@RequestMapping(value = "wxinvite")
	public String wxinvite(HttpServletRequest request, HttpServletResponse response,String inviteid,String invitedid,String payamt) {
		String id = UUID.randomUUID().toString().replace("-", "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String intime = df.format(new Date());
		String status = "0";
	    int result = hhxInviteService.saveInvite(id,inviteid,invitedid,intime,payamt,status);
		Map<String, String> map = new HashMap<String, String>();
		if(result>0) {
			map.put("result","true");
			map.put("msg","邀约成功");
			hhxUserService.updateInvite(invitedid);
		}else {
			map.put("result","false");
			map.put("msg","邀约失败");
		}
		
		return renderString(response, map);
	}
	
	@RequestMapping(value = {"wxlist", ""})
	public String wxlist(HttpServletRequest request, HttpServletResponse response,String inviteid) {
		List<Object[]> invitelist = hhxInviteService.getList(inviteid);
		return renderString(response, invitelist);
	}
	
	@RequestMapping(value = {"wxlist2", ""})
	public String wxlist2(HttpServletRequest request, HttpServletResponse response,String invitedid) {
		List<Object[]> invitedlist = hhxInviteService.getList2(invitedid);
		return renderString(response, invitedlist);
	}
	
	@RequestMapping(value = "updatecomment")
	public String updatecomment(HttpServletRequest request, HttpServletResponse response,String inviteid,String invitedid,String comment) {
	    int result = hhxInviteService.saveComment(inviteid,invitedid,comment);
		Map<String, String> map = new HashMap<String, String>();
//		if(result>0) {
			map.put("result","true");
			map.put("msg","评价成功");
//		}else {
//			map.put("result","false");
//			map.put("msg","邀约失败");
//		}
		
		return renderString(response, map);
	}
	

}